package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.HomeMapper;
import com.atguigu.spzx.manager.service.HomeService;
import com.atguigu.spzx.model.dto.chat.BussnessDto;
import com.atguigu.spzx.model.dto.order.createOrderDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.address.addressVo;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.sell.SellVo;
import com.atguigu.spzx.model.vo.chat.bussnessVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;
    @Override
    public List<SellVo> GetAllsell() {
        List<SellVo> list = homeMapper.GetAllsell();
        return list;
    }

    @Override
    public SellVo GetSellById(Integer id) {
        SellVo sellVo = homeMapper.GetSellById(id);
        return sellVo;
    }

    @Override
    public bussnessVo GetAvatar(String bussnessName) {
        bussnessVo bussnessVo = homeMapper.GetAvatar(bussnessName);
        return bussnessVo;
    }

    @Override
    public void CreateChat(BussnessDto bussnessDto) {
        SysUser sysUser = AuthContextUtil.get();
        Long userSId = sysUser.getId();
        String userSname = sysUser.getUserName();
        //查询插入的数据是否有重复
        Long existingChatId  = homeMapper.JudgeSame(userSId, bussnessDto.getBussnessId());
        // 判定 sysUserId 是否等于 bussnessId
        if (userSId.equals(bussnessDto.getBussnessId())) {
            // 返回自定义错误信息
            throw new GuiguException(ResultCodeEnum.CANNOT_CHAT_WITH_SELF);
        } else if (existingChatId != null) {
            // 如果 existingChatId 不为 null，说明记录已存在
            throw new GuiguException(ResultCodeEnum.CHAT_ALREADY_EXISTS); // 自定义错误信息
        } else {
            // 如果没有重复记录，则创建聊天
            homeMapper.CreateChat(bussnessDto, userSId, userSname);
            Long conversationId = homeMapper.JudgeSame(userSId, bussnessDto.getBussnessId());
            homeMapper.CreateMessage(conversationId, userSId);
        }
    }

    @Override
    public List<addressVo> GetUserAddress(Long id) {
        List<addressVo> address = homeMapper.GetUserAddress(id);
        return address;
    }

    @Override
    public void CreateOrder(createOrderDto createOrderDto) {
        SysUser sysUser = AuthContextUtil.get();
        Long buyofuserid = sysUser.getId();
        String buyOfusername = sysUser.getUserName();
        String useravatar = sysUser.getAvatar();
        createOrderDto.setBuyofuserid(buyofuserid);
        createOrderDto.setBuyOfusername(buyOfusername);
        createOrderDto.setUserAvatar(useravatar);

        //订单编号
        String ordernumber = UUID.randomUUID().toString();
        System.out.println(ordernumber);
        createOrderDto.setOrdernumber(ordernumber);

        String ifpayment = createOrderDto.getIfpayment();
        if (ifpayment.equals("true")){
            //创建已付款买家订单
            homeMapper.CreateBuyOrder(createOrderDto);
        }else {
            //创建未付款买家订单
            homeMapper.CreateNotpaymentBuyOrder(createOrderDto);
        }

    }

}
